/**
 * Copyright 2018-2019 jianggujin (www.jianggujin.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jianggujin.modulelink.config;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.jianggujin.modulelink.util.JAssert;

/**
 * 多版本Spring模块配置
 * 
 * @author jianggujin
 *
 */
public class JSpringMultiVersionModuleConfig extends JMultiVersionModuleConfig implements JSpringModuleConfigAdapter {
    /**
     * XML配置
     */
    private Set<String> xmlPatterns;
    /**
     * 需要排除的配置文件
     */
    private Set<String> exclusions;

    public JSpringMultiVersionModuleConfig(String name, String version) {
        super(name, version);
    }

    public JSpringMultiVersionModuleConfig(JMultiVersionModuleConfig moduleConfig) {
        super(moduleConfig.getModuleName(), moduleConfig.getModuleName());
        this.setDescription(moduleConfig.getDescription());
        this.setModuleUrls(moduleConfig.getModuleUrls());
        this.setScanPackages(moduleConfig.getScanPackages());
        this.setExclusionActions(moduleConfig.getExclusionActions());
    }

    /**
     * 获得XML配置
     * 
     * @return XML配置
     */
    @Override
    public Set<String> getXmlPatterns() {
        return xmlPatterns;
    }

    /**
     * 设置XML配置
     * 
     * @param xmlPatterns XML配置
     */
    public void setXmlPatterns(Set<String> xmlPatterns) {
        this.xmlPatterns = xmlPatterns;
    }

    /**
     * 添加XML配置
     * 
     * @param xmlPattern XML配置
     * @return {@link JSpringMultiVersionModuleConfig}
     */
    public JSpringMultiVersionModuleConfig addXmlPattern(String xmlPattern) {
        JAssert.checkNotNull(xmlPattern, "xmlPattern must not be null");
        if (this.xmlPatterns == null) {
            this.xmlPatterns = new HashSet<String>();
        }
        this.xmlPatterns.add(xmlPattern);
        return this;
    }

    /**
     * 获得需要排除的配置文件
     * 
     * @return 需要排除的配置文件
     */
    @Override
    public Set<String> getExclusions() {
        return exclusions;
    }

    /**
     * 设置需要排除的配置文件
     * 
     * @param exclusions 需要排除的配置文件
     */
    public void setExclusions(Set<String> exclusions) {
        this.exclusions = exclusions;
    }

    /**
     * 添加需要排除的配置文件
     * 
     * @param exclusion 需要排除的配置文件
     * @return {@link JSpringMultiVersionModuleConfig}
     */
    public JSpringMultiVersionModuleConfig addExclusion(String exclusion) {
        JAssert.checkNotNull(exclusion, "exclusion must not be null");
        if (this.exclusions == null) {
            this.exclusions = new HashSet<String>();
        }
        this.exclusions.add(exclusion);
        return this;
    }

    /**
     * 获得模块地址路径
     * 
     * @return 模块地址路径
     */
    @Override
    public List<String> getModuleUrlPath() {
        Set<URL> urls = this.getModuleUrls();
        if (urls == null) {
            return null;
        }
        List<String> moduleUrls = new ArrayList<String>();
        for (URL url : urls) {
            moduleUrls.add(url.toString());
        }
        return moduleUrls;
    }
}
